﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using BusinessLogic.BusinessEntity.Entities;
using UserInterface.ClientApplication.UIEntity;


namespace UserInterface.ClientApplication
{
	public partial class ClientEditForm : Form, IForm
	{
		ClientView client = new ClientView();
		private int ind = -1;
		public ClientEditForm()
		{
			InitializeComponent();
		}

		private void ClientEditForm_Load(object sender, EventArgs e)
		{
			try
			{
				ind = (int)fm.Param;
				if (ind != -1)
				{
					client = fm.GetClient(ind);
				}
			}
			catch (Exception)
			{
			}
			finally
			{
				InitBinding();
			}



		}

		private void InitBinding()
		{
			NameTextBox.DataBindings.Add("Text", client, "Name");
			AddressTextBox.DataBindings.Add("Text", client, "Address");
			PhoneTextBox.DataBindings.Add("Text", client, "Phone");
			FaxTextBox.DataBindings.Add("Text", client, "Fax");
			EmailTextBox.DataBindings.Add("Text", client, "Email");
			UNPTextBox.DataBindings.Add("Text", client, "UNP");
		}


		public FormManager fm { get; set; }


		public DialogResult Show(FormManager fm)
		{
			this.fm = fm;
			return ShowDialog();
		}


		private void CancelButton_Click(object sender, EventArgs e)
		{
			
			Close();
		}

		private void SaveButton_Click(object sender, EventArgs e)
		{
			if (ValidateChildren())
			{
				if(ind == -1)
				{
					fm.AddClient(client);
				}
				else
				{
					fm.SaveState();
				}
				Close();
			}else
			{
				DialogResult = System.Windows.Forms.DialogResult.None;
			}
		}

		private void EmailTextBox_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			var text = EmailTextBox.Text.Trim();
			if (text.Length != 0)
			{
				int pos = text.IndexOf('@');
				if ((pos == -1)
					 || (pos == 0)
					 || (pos == text.Length - 1)
					 || text.Substring(pos).IndexOf('.') == -1
					 || text.Substring(pos).IndexOf('.') == 1
					 || text.Substring(pos).IndexOf('.') == text.Substring(pos).Length - 1)
				{
					error = "Введите корректный e-mail";
					e.Cancel = true;
				}
				else
				{
					for (int i = 0; i < text.Length; i++)
					{
						if (char.IsLetter(text[i])
							|| text[i] == '@'
							|| text[i] == '.'
							|| char.IsDigit(text[i]))
						{
							continue;
						}
						error = "Введите корректный e-mail";
						e.Cancel = true;
						break;
					}
				}
			}
			ErrorProvider.SetError((Control)sender, error);
		}




		private void NameTextBox_Validating(object sender, CancelEventArgs e)
		{
			string error = null;

			var text = NameTextBox.Text.Trim();
			if(text.Length > 0)
			{
				for(int i = 0; i < text.Length; i++)
				{
					if(!char.IsLetterOrDigit(text[i])  && text[i] != ' ' && text[i] != '\'' && text[i] != '-')
					{
						error = "Введите корректное имя";
						e.Cancel = true;
						break;
					}
				}
			}
			else
			{
				error = "Введите корректное имя";
				e.Cancel = true;
			}
			ErrorProvider.SetError((Control)sender, error);
		}

		private void ClientEditForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			try
			{
				if (DialogResult != System.Windows.Forms.DialogResult.OK)
				{
					fm.LoadState();
				}
			}
			catch (Exception ex) { }
		}

		private void PhoneTextBox_Validating(object sender, CancelEventArgs e)
		{
			Regex regex = new Regex(@"^\+([\s\(\)\-]*\d[\s\(\)\-]*){12}$");
			string error = null;
			if (PhoneTextBox.Text.Length > 0 && !regex.IsMatch(PhoneTextBox.Text))
			{
				error = "Введите корректный телефон(12 цифр)";
				e.Cancel = true;
			}

			ErrorProvider.SetError((Control)sender, error);
		}

		private void UNPTextBox_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			
			if (UNPTextBox.Text.Length > 0)
			{
				if (UNPTextBox.Text.Length != 9)
				{
					error = "Введите корректный УНП(9 цифр)";
					e.Cancel = true;
				}
				for (int i = 0; i < UNPTextBox.Text.Length; i++)
				{
					if (!char.IsDigit(UNPTextBox.Text[i]))
					{
						error = "Введите корректный УНП(9 цифр)";
						e.Cancel = true;
					}
				}
			}

			ErrorProvider.SetError((Control)sender, error);
		}

	
	}
}
